<?php
// Parses the request URL into controller, action, and parameters.
class Router extends Object {

    var $webroot = null;
    
    var $base = null;
    
    var $url = null;
    
    var $admin = false;

    // Gets a reference to the Router object instance
    function &get_instance() {
        static $instance = array();
        if (!isset($instance[0]) || !$instance[0])
            $instance[0] =& new Router();
        return $instance[0];
    }

    // Returns this object's routes array. Returns false if there are no routes available.
    function connect($route) {
        
    }

    // Parses given URL and returns an array of controllers, action and parameters taken from that URL.
    function parse($url) {
        $params = null;
        $_this =& Router::get_instance();
        $urls = explode('/', $url);
        $i = 0;
        switch ($urls[$i]) {
            case ARLICLE_ADMIN:
                $params['controller'] = empty($urls[1]) ? 'login' : $urls[1];
                $params['action'] = empty($urls[2]) ? 'index' : $urls[2];
                $_this->set_webroot('arlicle/app/webroot/');
                $_this->admin = true;
                $i = 3;
            break;
            case 'rss2';
                $params['controller'] = 'rss2';
                $params['action'] = 'rss2';
                $_this->rss2 = true;
            break;
            default :
                $params['controller'] = 'template_loader';
                $params['template'] = empty($urls[$i]) ? 'index' : $urls[$i];
                $_this->set_webroot('www/themes/' . get_option('template') . '/');
            break;
        }

        if (isset($urls[$i])) {
            while (isset($urls[$i])) {
                $params['params'][] = $urls[$i];
                ++$i;
            }
        } else {
            $params['params'] = array();
        }
        return $params;
    }

    function is_admin() {
        $_this =& Router::get_instance();
        return $_this->admin;
    }

    function get_webroot() {
        $_this =& Router::get_instance();
        return $_this->webroot;
    }

    function get_plugin_webroot() {
        $_this =& Router::get_instance();
        return $_this->base. 'www/plugins/' . PLUGIN_DIR . '/app/webroot';
    }

    function set_webroot($url) {
        $_this =& Router::get_instance();
        $_this->webroot = $_this->base . $url;
    }

    function get_base() {
        $_this =& Router::get_instance();
        return $_this->base;
    }

    function set_base($base) {
        $_this =& Router::get_instance();
        $_this->base = $base;
    }

    function get_url() {
        $_this =& Router::get_instance();
        return $_this->url;
    }

    function set_url($url) {
        $_this =& Router::get_instance();
        $_this->url = $url;
    }

    function get_action() {
        
    }

    function set_action() {

    }
}
?>
